package com.hsyt.pfc.support.util;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @ClassName: EnumToDataUtil
 * @Description: 泛型类，处理枚举数据
 * @Author: wangze@hsyuntai.com
 * 2015年4月9日 下午8:29:57
 */
public class EnumToDataUtil {

    private static final Logger log = LoggerFactory.getLogger(EnumToDataUtil.class);

    public static String getDHEnumsValuesFromEnum(Class<?> clazz) {
        Map map = new LinkedHashMap();
        try {
            Method[] methods = clazz.getDeclaredMethods();
            if (clazz.isEnum()) {
                List<?> list = Arrays.asList(clazz.getEnumConstants());
                for (Object object : list) {
                    StringBuffer key = new StringBuffer("");
                    StringBuffer value = new StringBuffer("");
                    for (Method method : methods) {
                        if (method.getName().startsWith("getCode")) {
                            key.append(method.invoke(object));
                        }
                        if (method.getName().startsWith("getName")) {
                            value.append(method.invoke(object));
                        }
                    }
                    if (!"".equals(key.toString())) {
                        map.put(key.toString(), value.toString());
                    }
                }
            }
        } catch (Exception e) {
            log.error("枚举类转化成dhtmlx串失败！", e);
        }
        return getDHEnumsValuesByMap(map, "", "");
    }
    public static String getDHEnumsValuesFromEnum(Class<?> clazz, String valueField, String textField) {
        Map map = new LinkedHashMap();
        String valueMethodName = "get" + (StringUtils.isBlank(valueField) ? "Code" : StringUtils.capitalize(valueField));
        String textMethodName = "get" + (StringUtils.isBlank(valueField) ? "Name" : StringUtils.capitalize(textField));
        try {
            Method[] methods = clazz.getDeclaredMethods();
            if (clazz.isEnum()) {
                Object key =  null;
                Object value = null;
                List<?> list = Arrays.asList(clazz.getEnumConstants());
                for (Object object : list) {

                    for (Method method : methods) {
                        if (StringUtils.equals(method.getName(), valueMethodName)) {
                            key = method.invoke(object);
                        }
                        if (StringUtils.equals(method.getName(), textMethodName)) {
                            value = method.invoke(object);
                        }
                    }
                    if (key != null && value != null) {
                        map.put(key.toString(), value.toString());
                    }
                }
            }
        } catch (Exception e) {
            log.error("枚举类转化成dhtmlx串失败！", e);
        }
        return getDHEnumsValuesByMap(map, "", "");
    }
    public static String getDHEnumsValuesByMap(Map map, String selectValue, String selectText) {
        if (map == null || map.isEmpty()) {
            return "([[\'" + selectValue + "\', \'" + selectText + "\']])";
        }
        StringBuffer returnStr = new StringBuffer("([[\'" + selectValue + "\', \'" + selectText + "\'],");
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            returnStr.append("[\'");
            returnStr.append(entry.getKey());
            returnStr.append("\',\'");
            returnStr.append(entry.getValue());
            returnStr.append("\']");
            if (iter.hasNext()) {
                returnStr.append(",");
            }
        }
        returnStr.append("])");
        return returnStr.toString();
    }
    public static String getComboData(List listData,String valueField, String textField,String selectValue){
        StringBuilder returnStr = new StringBuilder("([[\'").append(selectValue== null ? "": selectValue).append("\', \'--请选择--\'],");
        if(listData==null||listData.isEmpty()){
            return "([[\'"+(selectValue== null ? "": selectValue)+ "\', \'--请选择--\']])";
        } else {
            Object value = null;
            Object name = null;
            try {
                for (int i = 0, n = listData.size(); i < n; i++) {
                    Object dto = listData.get(i);
                    value = PropertyUtils.getProperty(dto, valueField);
                    name = PropertyUtils.getProperty(dto, textField);
                    if (value != null) {
                        returnStr.append("[\'");
                        returnStr.append(value);
                        returnStr.append("\',\'");
                        returnStr.append(name);
                        returnStr.append("\']");
                        if (i != n - 1) {
                            returnStr.append(",");
                        }
                    }
                }
            }catch (Exception e) {
                log.error("转化数据错误:" + e.getMessage());
            }
            returnStr.append("])");
        }
        return returnStr.toString();
    }
}
